package com.chenglu.android.mine.member;


import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chenglu.android.R;
import com.chenglu.android.common.base.BaseActionBarActivity;
import com.chenglu.android.common.base.Global;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.mine.bean.FansListBean;
import com.chenglu.android.mine.bean.MemberCenterBean;
import com.chenglu.android.mine.bean.PosterBean;
import com.chenglu.android.mine.bean.ReturnStringBean;
import com.chenglu.android.mine.member.adapter.FansListAdapter;
import com.chenglu.android.mine.member.mvp.contract.MemberContract;
import com.chenglu.android.mine.member.mvp.presenter.MemberPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author: lyj
 * @Created: by Copyright 2023 6 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：消费列表
 */
public class ConsumeListActivity extends BaseActionBarActivity<MemberPresenter> implements MemberContract.View {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout smartrefreshlayout;
    @BindView(R.id.ll_loading_data)
    LinearLayout llLoadingData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_loading_no_data)
    LinearLayout llLoadingNoData;

    private FansListAdapter mAdapter;

    private  int current = 1;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_fans_list;
    }

    // fanstype 判断 1 时间 2 价格
    private int fansType = 2;

    @Override
    protected void initView() {
        setCenterText("消费列表");
        mPresenter = new MemberPresenter();
        mPresenter.attachView(this);

        initRefreshLayout();

        initAdapter();

        //消费列表
        mPresenter.getConsumerPage(getToken(),current, Constant.SIZE);
    }

    private void initAdapter() {

        mAdapter = new FansListAdapter(mContext,R.layout.item_fans_list, list,fansType);

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter);
    }



    List<FansListBean.Data.Records> list = new ArrayList<>();


    //刷新
    private void initRefreshLayout() {

        smartrefreshlayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //id 需要传参过来
                current = 1;
                //需要清空数据再加
                list.clear();
                mPresenter.getFansPage(getToken(), current,Constant.SIZE);


            }
        });
        smartrefreshlayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                current += 1;
                mPresenter.getFansPage(getToken(), current,Constant.SIZE);

            }
        });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }



    @Override
    public void getLevelDescriptions(ReturnStringBean bean) {

    }

    @Override
    public void getMemberCenterInfo(MemberCenterBean bean) {

    }

    @Override
    public void getFansPage(FansListBean bean) {

    }

    @Override
    public void getConsumerPage(FansListBean bean) {
        stopRefresh(smartrefreshlayout);
        if(bean.getData().getTotal()>0){
            llLoadingData.setVisibility(View.VISIBLE);
            llLoadingNoData.setVisibility(View.GONE);
            for (int i = 0; i <bean.getData().getRecords().size() ; i++) {
                list.add(bean.getData().getRecords().get(i));
            }

            if(bean.getData().getTotal() < Constant.SIZE){
                smartrefreshlayout.setEnableRefresh(false);
            }
            if(current *Constant.SIZE > bean.getData().getTotal()){
                smartrefreshlayout.setEnableLoadMore(false);
                View inflate = Global.inflate(R.layout.loadmore_addfooter_layout);
                inflate.findViewById(R.id.tv_no_more).setBackgroundColor(getResources().getColor(R.color.white));
                mAdapter.addFooterView(inflate);
            }

        }else {
            llLoadingData.setVisibility(View.GONE);
            llLoadingNoData.setVisibility(View.VISIBLE);
        }

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void getPromotionPoster(PosterBean bean) {

    }
}
